package com.weiyi.mashangzhu.Roomer.Activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Roomer.Adapter.RecommendAdapter2;
import com.weiyi.mashangzhu.Roomer.Bean.ThemeHavePageBean;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RecommendActivity extends BaseActivity {


    @BindView(R.id.refresh_list)
    PullToRefreshListView refreshList;
    List<ThemeHavePageBean.DataBean.HouselistBean> list = new ArrayList<>();
    RecommendAdapter2 collectadapter = null;
    String id = "";
    int page = 1;
    String info = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recommend);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        info = intent.getStringExtra("info");
        Log.e("rrrrrrrr",id+","+info);

        init(1);
        refreshList
                .setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
                    @Override
                    public void onPullDownToRefresh(
                            PullToRefreshBase<ListView> refreshView) {
                        Log.e("TAG", "onPullDownToRefresh");
                        //这里写下拉刷新的任务
                        new DownGetDataTask().execute();
                    }

                    @Override
                    public void onPullUpToRefresh(
                            PullToRefreshBase<ListView> refreshView) {
                        Log.e("TAG", "onPullUpToRefresh");
                        new UpGetDataTask().execute();
                    }
                });

    }

    @Override
    protected void onResume() {
        super.onResume();
        init(page);
    }

    public void init(final int page) {
        //获取活动房屋列表
        if (info.equals("activity")) {
            OkHttpUtils.get().addParams("token", SPUtils.get(this, "token", "") + "").addParams("id", id).addParams("pagenum", page + "").url(API.BaseApi_R + API.Main_Activity_List_Api).build().execute(new StringCallback() {
                @Override
                public void onError(Request request, Exception e) {
                    T.showShort(RecommendActivity.this, "网络连接失败，请检查一下网络设置");
                }

                @Override
                public void onResponse(String response) {
                    Gson gson = new Gson();
                    if (page == 1) {
                        list.clear();
                        list = gson.fromJson(response, ThemeHavePageBean.class).getData().getHouselist();
                        collectadapter = new RecommendAdapter2(list, RecommendActivity.this,"");
                        refreshList.setAdapter(collectadapter);
                    } else {
                        list.addAll(gson.fromJson(response, ThemeHavePageBean.class).getData().getHouselist());
                        collectadapter.notifyDataSetChanged();
                    }

                }
            });
       //获取主题房源列表
        } else if (info.equals("themehouse")) {
            OkHttpUtils.get().addParams("token", SPUtils.get(this, "token", "") + "").addParams("pagenum", page + "").url(API.BaseApi_R + API.Main_ThemeHouse_Api).build().execute(new StringCallback() {
                @Override
                public void onError(Request request, Exception e) {
                    T.showShort(RecommendActivity.this, "网络连接失败，请检查一下网络设置");
                }

                @Override
                public void onResponse(String response) {
                    Gson gson = new Gson();
                    if (page == 1) {
                        list.clear();
                        list = gson.fromJson(response, ThemeHavePageBean.class).getData().getHouselist();
                        collectadapter = new RecommendAdapter2(list, RecommendActivity.this,"");
                        refreshList.setAdapter(collectadapter);
                    } else {
                        list.addAll(gson.fromJson(response, ThemeHavePageBean.class).getData().getHouselist());
                        collectadapter.notifyDataSetChanged();
                    }
                }
            });
       //获取热门房屋列表
        } else if (info.equals("hothouse")) {
            OkHttpUtils.get().addParams("token", SPUtils.get(this, "token", "") + "").addParams("pagenum", page + "").url(API.BaseApi_R + API.Main_HotHouse_Api).build().execute(new StringCallback() {
                @Override
                public void onError(Request request, Exception e) {
                    T.showShort(RecommendActivity.this, "网络连接失败，请检查一下网络设置");
                }

                @Override
                public void onResponse(String response) {
                    Gson gson = new Gson();
                    if (page == 1) {
                        list.clear();
                        list = gson.fromJson(response, ThemeHavePageBean.class).getData().getHouselist();
                        collectadapter = new RecommendAdapter2(list, RecommendActivity.this,"");
                        refreshList.setAdapter(collectadapter);
                    } else {
                        list.addAll(gson.fromJson(response, ThemeHavePageBean.class).getData().getHouselist());
                        collectadapter.notifyDataSetChanged();
                    }

                }
            });

        }

    }

    private class DownGetDataTask extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... params) {
            try {
                Thread.sleep(1000);


            } catch (InterruptedException e) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            page = 1;
            init(page);
            refreshList.onRefreshComplete();
        }
    }

    private class UpGetDataTask extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... params) {
            try {
                Thread.sleep(1000);

            } catch (InterruptedException e) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            page += 1;
            init(page);
            refreshList.onRefreshComplete();
        }
    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }
}
